Components.utils.import("resource://gre/modules/Services.jsm");

var OpenID4Fennec_Content = {
	load : function () {
		//OpenID4Fennec_Content.log("load");
		addMessageListener("OpenID4Fennec:chromeTapLong", OpenID4Fennec_Content.chromeTapLong);
		addMessageListener("OpenID4Fennec:chromeToolbarClick", OpenID4Fennec_Content.chromeToolbarClick);
	},

	chromeToolbarClick : function(msg) {
		var preferredOpenidProvider = msg.json.preferredOpenidProvider
		//OpenID4Fennec_Content.log("chromeToolbarClick preferredOpenidProvider=" + preferredOpenidProvider);
		try {
			Components.utils.import("resource://openidnascar/inputfields.jsm");
			var inputField = findBestInputField(content.document, OpenID4Fennec_Content.log);
//			var inputField = content.document.getElementById("openid_url");
			if (inputField) {
				//OpenID4Fennec_Content.log("chromeToolbarClick inputField");
		        preferredOpenidProvider = unescape(preferredOpenidProvider);
		        var onchange = inputField.getAttribute("onchange");
		        if (onchange) {
		          // notify element of change
		          var evnt = document.createEvent( "Event");
		          evnt.initEvent( "change", true, true);
		          inputField.dispatchEvent(evnt);
		          // but do not submit form
		        } else {
		          var form = inputField.form;
		          if (!form) {
		            inputField.setAttribute("value", preferredOpenidProvider);
		            {
		              // special janrain code
		              var parentDoc = inputField.ownerDocument; // not doc because of frames
		              var wa_signin = parentDoc.getElementById("wa_signin"); // janrain
		              if (wa_signin) {
		                var event = parentDoc.createEvent( "Event");
		                event.initEvent( "click", true, true);
		                wa_signin.dispatchEvent( event);
		              }
		            }            
		            //OpenID4Fennec_Content.log("chromeToolbarClick no form in " + inputField.ownerDocument.location.href);
		            return;
		          }
		          inputField.setAttribute("value", preferredOpenidProvider);
		          form.submit();
		        }

			} else {
				//OpenID4Fennec_Content.log("chromeToolbarClick !inputField");
			}
		} catch(e) {
		  Components.utils.reportError(e);
		}
	},
	
	chromeTapLong : function (msg) {
		try {
			//OpenID4Fennec_Content.log("chromeTapLong");
			var target = elementFromPoint(msg.json.x, msg.json.y);

			if (target) {
				//OpenID4Fennec_Content.log("chromeTapLong target.nodeName=" + target.nodeName);
				if (target.nodeName.toLowerCase() === 'input') {
					if (!target.getAttribute("id")) {
						target.setAttribute("id", "OpenID4Fennec-" + Math.floor(Math.random() * 10000));
					}

					sendAsyncMessage("OpenID4Fennec:tapLong", { "fieldId" : target.getAttribute("id") } );
				}
			} else {
				OpenID4Fennec_Content.log("chromeTapLong no target");
			}
		} catch (e) {
		  Components.utils.reportError(e);
		}
	},
	
	log : function (msg) {
		try {
			Services.console.logStringMessage("OpenID4Fennec_Content: " + msg);
		} catch(e) {}
		try {
			dump(msg);
		} catch(ee) {}
	}
	
};

OpenID4Fennec_Content.load();